package com.misday.eatup.model;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;


import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class UpdateDatabase {
	
	private static final String TAG = "UpdateDatabase";
	
	private static final String DEFAULT_DATA = "foods.xml";
		
	UpdateDatabase() {};
	
	public static void update(Context context, SQLiteDatabase db) {
		SAXParserFactory parserFactory = SAXParserFactory.newInstance();
		FoodsHandler foodsHandler = new FoodsHandler();
		FoodsDatabaseHelper mFDBHelper = new FoodsDatabaseHelper(context);

		try {
			//Log.i(TAG, "try...");
			InputStream foodsStream = context.getAssets().open(DEFAULT_DATA);
			InputSource input = new InputSource(foodsStream);
			//Log.i(TAG, "parse");
			XMLReader xmlReader = parserFactory.newSAXParser().getXMLReader();
			xmlReader.setContentHandler(foodsHandler);
			
			//Log.i(TAG, "parse start");
			xmlReader.parse(input);
			//Log.i(TAG, "parse over");
			
			//SQLiteDatabase db = mFDBHelper.getWritableDatabase();

			List<ContentValues> conflictList = foodsHandler.getConflictList();
			Log.i(TAG, "conflictList count = " + conflictList.size());
			for (int i = 0; i < conflictList.size(); i++)  {
				ContentValues conflict = conflictList.get(i);
				Log.i(TAG, "food1 = " + conflict.getAsString(FoodsColumns.FOOD1));
				Log.i(TAG, "food2 = " + conflict.getAsString(FoodsColumns.FOOD2));
				Log.i(TAG, "priority = " + conflict.getAsString(FoodsColumns.PRIORITY));
				Log.i(TAG, "detail = " + conflict.getAsString(FoodsColumns.DETAIL));
				
				mFDBHelper.insert(db, 
						conflict.getAsString(FoodsColumns.FOOD1), 
						conflict.getAsString(FoodsColumns.FOOD2), 
						conflict.getAsString(FoodsColumns.PRIORITY), 
						conflict.getAsString(FoodsColumns.DETAIL));
				
			}
			
			List<ContentValues> unsuitabelList = foodsHandler.getUnsuitableList();
			Log.i(TAG, "unsuitabelList count = " + unsuitabelList.size());
			for (int i = 0; i < unsuitabelList.size(); i++)  {
				ContentValues unsuitable = unsuitabelList.get(i);
				Log.i(TAG, "food = " + unsuitable.getAsString(FoodsColumns.FOOD));
				Log.i(TAG, "priority = " + unsuitable.getAsString(FoodsColumns.PRIORITY));
				Log.i(TAG, "detail = " + unsuitable.getAsString(FoodsColumns.DETAIL));
				
				
				mFDBHelper.insert(db, 
						unsuitable.getAsString(FoodsColumns.FOOD), 
						unsuitable.getAsString(FoodsColumns.PRIORITY), 
						unsuitable.getAsString(FoodsColumns.DETAIL));
				
			}
			
			foodsStream.close();
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
